<template>
	<page-meta :page-style="'overflow:'+(popShow?'hidden':'visible')"></page-meta>
	<view class="money-content">
		<headassy title="入库列表" searchTitle="入库单据" @goSearch="goSearch" @openSideBar="getAllSearch" :isTab="false"></headassy>
		<view class="nav-status text-orange">
			当前状态有{{count}}个入库单
		</view>
		<scroll-view>
			<view class="card-list">
				<view class="card mt-5" v-for="item in pageList" :key="item.id">
					<view class="top u-flex-between">
						<view class="left u-flex">
							<view class="text-info ">
								{{item.singleNum}}
							</view>
						</view>
						<view class="cus-blue-tag">
							{{item.documentStatus}}
						</view>
					</view>
					<view class="center">
						<view class="title u-flex-between">
							<view class="title-left" style="font-size: 14px;">
								客户/网点:
								<span style="margin-left: 14rpx;">{{item.operator}}</span>
							</view>
						</view>
						<view class="text">
							出库仓库:
							<span style="margin-left: 14rpx;">{{item.wareName}}</span>
						</view>
						<view class="text">
							制单人:
							<span style="margin-left: 14rpx;">{{item.billingPeople }}</span>
						</view>
						<view class="text">
							制单时间:
							<span style="margin-left: 14rpx;">{{item.billingTime }}</span>
						</view>
						<view class="text">
							最后更新时间:
							<span style="margin-left: 14rpx;">{{item.billingTime }}</span>
						</view>
					</view>
					<!-- <view class="down u-flex-content-end">
						<view class="right u-flex">
							<view class="cus-oragen-btn" style="margin-left: 12px;" @click="goCkSh">
								审核
							</view>
						</view>
					</view> -->
					
					<!-- <view class="down u-flex-content-end">
						<view class="right u-flex">
							<view class="cus-btn text-blue mr-5">
								配件费:{{item.spareFee}}
								<span style="color: red;">¥</span>
							</view>
							<view class="cus-btn text-blue mr-5">
								合计:{{item.workFee}}
								<span style="color: red;">¥</span>
							</view>
						</view>
					</view> -->
				</view>
			</view>
			<view class="example-body">
				<uni-load-more :status="status" :content-text="contentText"/>
			</view>
		</scroll-view>
		
		<!-- 侧边栏 -->
		<sidebarpop
			ref="page-sidebar"
			ptitle="筛选"
			isSlot="true"
			isDate="true"
			:height="(parseInt(system.safeArea.top) + 44)"
			:sidebarList="JSON.stringify(sidebarList)"
			@closeclick="closePop"
			@radioClick="radioClick"
			@confirmDate="radioClick"
			@clearSelect="clearSelect"
			@saveclick="saveSelect"
		>
		</sidebarpop>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				tabIndex: 0,
				count: '',

				page: {
					custAccId: '',
					pageNum: 1,
					pageSize: 20,
					type: "0",
					isDelete: "0",
				},
				data: {
					isErp: "",				// 是否erp单据 ：erp：true  系统单据：false
					networkId: "",			// 网点id
					wareId: '',				// 仓库id
					typeSpecific: "",		// 具体类型  例如 常规出库 售后出库 销售订单 零售结存等  目前无用
					startDate: '',			// 开始日期
					endDate: '',			// 结束时间
					billingPeople: '',		// 制单人
					singleNum: ''			// 单号搜索
					
				},
				pageList: [],				// 页面数据
				
				system: uni.getSystemInfoSync(),
				
				sidebarList: [],			// 侧边栏数据
				popShow: false,				// 弹窗触发时为true
				popShowName: '',			// 弹窗名称
				
				// 搜索项列表
				formTypeList: [],			// 单据类型列表
				wdList: [],					// 网点列表
				wareTypeList: [],			// 业务类型列表
				waresList: [],				// 仓库列表
				
				// 加载动作
				status: 'loading',			//上拉加载
				contentText: {
					contentdown: '查看更多',
					contentrefresh: '加载中',
					contentnomore: '没有更多'
				},
			};
		},
		onLoad() {
			this.page.custAccId = uni.getStorageSync('custAccid');
			this.getPullRefresh();
		},
		// 下拉刷新
		onPullDownRefresh() {
			this.getPullRefresh()
		},
		// 触底监听
		onReachBottom() {
			if(this.status != 'noMore') {
				return 
			}
			console.log('触底，列表分组总数:'+Math.ceil(this.count / 20),'当前列表:'+this.page.pageNum);
			if(this.page.pageNum < Math.ceil(this.count / 20)) {
				this.page.pageNum++;
				this.getPageList('refresh');
			}
		},
		methods: {
			// 下拉刷新请求数据
			getPullRefresh() {
				console.clear();
				setTimeout(()=>{
					uni.stopPullDownRefresh();
				}, 1500);
				this.page.pageNum = 1;
				this.getSelectList();
				this.getPageList();
			},
			// 获取出库列表
			async getPageList(type) {
				if(type != 'refresh') {
					this.pageList = [];
					this.count = 0;
					this.status = 'loading';
				}
				const res = await uni.$api.logistics.getWareRecord({
					...this.page,
					...this.data
				})
				if(type == 'refresh') {
					this.pageList.push(...res.data);
				}else {
					this.pageList = res.data
				}
				this.status = 'noMore';
				this.count = res.count
				uni.stopPullDownRefresh()
			},
			// 获取搜索项列表
			async getSelectList() {
				// 获取网点搜索项
				const wdList = await uni.$api.work.getWorkwd({
					custAccId: this.page.custAccId
				});
				this.wdList = wdList.data.map((val => {
					val.ischeck = false;
					val.label = val.title
					return val;
				}));
				// 获取业务类型搜索项
				const wareTypeList = await uni.$api.currency.getWareType(this.page.custAccId);
				this.wareTypeList = wareTypeList.data.map((val => {
					val.ischeck = false;
					val.label = val.title
					return val;
				}));
				// 获取仓库搜索项
				const waresList = await uni.$api.currency.getWares(this.page.custAccId);
				this.waresList = waresList.data.map((val => {
					val.ischeck = false;
					val.title = val.text
					return val;
				}));
				// 设置单据类型搜索项
				this.formTypeList = [
					{ischeck: false,title: 'erp',value: 'true'},
					{ischeck: false,title: '系统单据',value: 'false'},
					{ischeck: false,title: '全部单据',value: ''},
				];
			},
			
			// 初始化侧边栏数据
			sidebarInit() {
				const sidebarLable = ['单据类型','网点','仓库','业务'];
				const sidebarList = [this.formTypeList,this.wdList,this.waresList,this.wareTypeList];
				this.sidebarList = [];
				sidebarLable.forEach((val,index) => {
					this.sidebarList.push({
						selectEle: null,
						isShowList: false,
						title: val,
						isModelShow: true,
						data: this.handleSidebarData(sidebarList[index])
					});
				})
				
			},
			handleSidebarData(res = []) {
				let data = res;
				data.forEach(val => {
					val.ischeck = false;
					if(!val.title) {
						if(val.nameVal) {
							val.title = val.nameVal
						}
						if(val.name) {
							val.title = val.name
						}
					}
				})
				return data
			},
			// 侧边栏模块标签点击
			radioClick(item,index,isRepeat) {
				console.log('侧边栏模块标签点击----',item,index,isRepeat)
				switch (index) {
					case 0: // 单据类型
						this.data.isErp = isRepeat ? '' : item.value;
						break;
					case 1: // 网点
						this.data.networkId = isRepeat ? '' : item.idVal;
						break;
					case 2: // 仓库
						this.data.wareId = isRepeat ? '' : item.id;
						break;
					case 3:	// 业务
						this.data.typeSpecific = isRepeat ? '' : item.label;
						break;
					case 4:	// 日期
						this.data.startDate = item.startTime;
						this.data.endDate = item.endTime;
						break;
				}
			},
			// 确认筛选
			saveSelect() {
				this.page.pageNum = 1;
				this.getPageList();
				this.closePop();
			},
			// 清除数据
			clearSelect() {
				Object.keys(this.data).forEach(key => {
					this.data[key] = key != 'serviceId' ? '' : 1
				})
			},
			
			// 搜索
			goSearch(e) {
				this.data.singleNum = e;
				this.saveSelect();
			},
			
			// 跳转出库审核
			goCkSh() {
				uni.navigateTo({
					url: '/pagesA/pages/factory/outboundaudit'
				})
			},
			// 打开侧边栏
			getAllSearch() {
				this.openModel('page-sidebar');
				setTimeout(() => {
					this.sidebarInit();
				})
			},
			// 新开弹窗
			openModel(ref) {
				if(this.$refs[ref]) {
					this.popShow = true;
					this.popShowName = ref;
					this.$refs[ref].pupshow = true;
				}
			},
			// 关闭弹窗
			closePop() {
				this.popShowName = '';
				this.popShow = false;
			},
		},

	}
</script>

<style lang="scss">
	// 去掉顶栏固定
	/deep/.u-navbar--fixed {
		position: static !important;
	}
	// 去掉默认背景色
	/deep/.u-popup__content {
		background-color: transparent !important;
	}
	.money-content {
		// height: 100vh;
		background-color: $uni-bg-color-grey;

		.u-nav-slot {
			display: flex;
			align-items: center;
		}

		.tabs {
			background-color: #fff;
			font-size: 28rpx;
			width: 100%;
		}

		.nav-status {
			height: 100rpx;
			line-height: 100rpx;
			text-align: center;
			font-size: 28rpx;
		}

		.card-list {
			background: $uni-bg-color-grey;
			padding: 0 28rpx;

			.card {
				padding: 22rpx 28rpx;
				background: #fff;
				border-radius: 10rpx;
				margin-bottom: 28rpx;

				.top {
					border-bottom: solid 1px #f5f5f5;
					padding-bottom: 10px;

					.left {
						.text-info {
							font-size: 28rpx;
							font-weight: 700;
						}
					}
				}

				.center {
					padding-top: 10px;
					padding-bottom: 10px;

					.title {
						.title-left {
							// font-size: 28rpx;
							color: gray;
						}

						.title-right {
							// font-size: 28rpx;
						}
					}

					.text {
						font-size: 28rpx;
						color: gray;
						white-space: nowrap;
						text-overflow: ellipsis;
						overflow: hidden;
					}
				}

				.down {
					border-top: solid 1px #f5f5f5;
					padding-top: 12px;
				}
			}
		}
	}
</style>